package basic.study.leetcode;

import basic.study.leetcode.Mid114.TreeNode;
/**
 * @ClassName Mid236
 * @Description 二叉树的公共祖先
 * @Company inspur
 * @Author Kevin
 * @Date 2020/7/9 10:07
 * @Version 1.0
 */
public class Mid236 {
    class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if (root == null || root == p || root == q) {
                return root;
            }
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if (left != null && right != null) {
                return root;
            }
            if (left != null) {
                return left;
            }
            if(right != null) {
                return right;
            }
            return null;
        }
    }
}
